package com.erp.erp_ui.warehouse.controller;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.warehouse.req.StatisticsTransferAndContractInfoReq;
import com.erp.erp_entitys.warehouse.req.StatisticsTransferInStockInfoReq;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.*;
import com.erp.erp_ui.warehouse.service.TransferService;
import com.erp.erp_ui.warehouse.vo.*;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;

/**
 * @ClassName : TransferController
 * @Description : 调拨控制层
 * @Author : lst
 * @Date: 2020-07-30 15:40
 */
@Controller
@RequestMapping(value = "transfer")
public class TransferController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(TransferController.class);

    /**
     * 调拨业务逻辑接口
     */
    private final TransferService transferService;

    @Autowired
    public TransferController(TransferService transferService) {
        this.transferService = transferService;
    }

    /**
     * 分页获取调拨集合
     *
     * @param req 查询条件实体
     * @return 调拨集合
     */
    @GetMapping("listByPage")
    @ResponseBody
    public R listByPage(TransferReq req) {
        LOGGER.info("仓库管理-调拨-获取分页调拨信息 req=" + req);
        try {
            PageInfo<TransferVo> transferVoPageInfo = transferService.getListByPage(req);
            return R.build(0, "", transferVoPageInfo.getTotal(), transferVoPageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取分页调拨信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除调拨信息
     *
     * @param id     调拨id
     * @param status 状态(1.草稿箱 2.待批箱 3.已批箱 4.完成箱 5.垃圾箱)
     * @return
     */
    @PostMapping("/deleteTransfer")
    @ResponseBody
    public R deleteTransfer(@RequestBody Integer id, @RequestBody Integer status) {
        try {
            LOGGER.info("仓库管理-调拨-删除调拨信息 id=" + id + ",status=" + status);
            transferService.deleteTransfer(id, status);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-删除调拨信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 合同置空获取详情
     *
     * @param id 调拨id
     * @return
     */
    @PostMapping("/detail/getTransferDetailInfo")
    @ResponseBody
    public R getTransferDetailInfo(@RequestBody Integer id) {
        try {
            LOGGER.info("仓库管理-调拨-获取详情 id=" + id);
            TransferDetailVo vo = transferService.getTransferDetailInfo(id, false);
            return R.build(vo);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 调拨获取详情
     *
     * @param id 调拨id
     * @return
     */
    @PostMapping("/detail/updateTransferInfo")
    @ResponseBody
    public R updateTransferInfo(@RequestBody Integer id) {
        try {
            LOGGER.info("仓库管理-调拨-合同置空编辑 id=" + id);
            TransferDetailVo vo = transferService.getTransferDetailInfo(id, true);
            return R.build(vo);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-合同置空编辑异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * FTP文件上传
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST, consumes = "multipart/*")
    @ResponseBody
    public R transferFileUpload(MultipartHttpServletRequest request) {
        LOGGER.info("仓库管理-调拨-FTP文件上传");
        try {
            String ftpPath = getFtpPath();
            UserEntity user = (UserEntity) this.getBean("user");
            String operator = user.getName();
            String operatorNo = user.getCode();
            List<AnnexVo> annexVos = transferService.transferFileUpload(request, ftpPath, operator, operatorNo);
            return R.build(annexVos);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-FTP文件上传异常:" + e);
            return R.buildError(ResultStatusEnum.UPLOAD_FILE_ERROR);
        }
    }

    /**
     * ftp路径，/upload/部门编码/员工编码
     *
     * @return
     */
    private String getFtpPath() {
        try {
            UserEntity user = (UserEntity) this.getBean("user");
            String code = user.getCode();
            String departmentCode = user.getDepartmentCode();
            return "/data0/AllPath/upload/" + departmentCode + "/" + code;
        } catch (Exception e) {
            e.printStackTrace();
            return "/data0/AllPath/upload";
        }

    }

    /**
     * 获取库存产品
     *
     * @param req 查询参数
     * @return 库存产品对象集合
     */
    @GetMapping("/add/getStockInfoByPage")
    @ResponseBody
    public R getStockInfo(InStockProductReq req) {
        LOGGER.info("仓库管理-调拨-获取入库产品 req=" + req);
        try {
            PageInfo<InStockProductVo> productVos = transferService.getStockInfo(req);
            return R.build(0, "", productVos.getTotal(), productVos.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取入库产品异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 获取销售合同主表信息
     *
     * @param req 查询条件
     * @return 销售合同信息
     */
    @GetMapping("/add/getSaleContractByPage")
    @ResponseBody
    public R getSaleContractByPage(ImportContractInfoReq req) {
        try {
            LOGGER.info("仓库管理-调拨-获取销售合同主表信息 req=" + req);
            //通过销售合同导入库存数据
            PageInfo<ImportContractInfoVo> contractInfoVoPageInfo = transferService.getStockBySaleContract(req);
            return R.build(0, "", contractInfoVoPageInfo.getTotal(), contractInfoVoPageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取销售合同主表信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取销售合同产品信息
     *
     * @param id 销售合同id
     * @return 销售合同产品对象集合
     */
    @GetMapping("/add/getSaleContractProductByPage")
    @ResponseBody
    public R getSaleContractProductByPage(Integer id) {
        try {
            LOGGER.info("仓库管理-调拨-获取销售合同产品信息 id=" + id);
            if (null == id) {
                return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
            }
            //通过销售合同导入库存数据
            PageInfo<ImportContractProductInfoVo> contractInfoVoPageInfo = transferService.getSaleContractProductByPage(id);
            return R.build(0, "", contractInfoVoPageInfo.getTotal(), contractInfoVoPageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取销售合同产品信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 根据销售合同id集合获取库存集合信息
     *
     * @param ids 销售id集合
     * @return 库存集合信息
     */
    @PostMapping("/add/getStockListByContractIds")
    @ResponseBody
    public R getStockListByContractIds(@RequestBody Integer[] ids) {
        try {
            LOGGER.info("仓库管理-调拨-根据销售合同id集合获取库存集合信息 ids=" + ids);
            List<ImportContractProductInfoVo> infoVos = transferService.getStockListByContractIds(ids);
            return R.build(infoVos);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-根据销售合同id集合获取库存集合信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 合同置空新增、修改 调拨新增、修改
     *
     * @param req 新增、修改入参
     * @return
     */
    @PostMapping("/contractInitAdd")
    @ResponseBody
    public R contractInitAdd(@Validated @RequestBody AddTransferReq req) {
        try {
            LOGGER.info("仓库管理-调拨-合同置空新增、修改 调拨新增、修改 req=" + req.toString());
            UserEntity user = (UserEntity) this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return transferService.contractInitAdd(req, name, code);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-合同置空新增、修改 调拨新增、修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 整个调拨单整体作废
     *
     * @param transferId 调拨id
     * @return 作废信息
     */
    @GetMapping("/invoiceBatchInvalid")
    @ResponseBody
    public R invoiceBatchInvalid(Integer transferId) {
        try {
            LOGGER.info("仓库管理-调拨-整个单据作废 transferId=" + transferId);
            return transferService.invoiceBatchInvalid(transferId);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-整个单据作废异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 单个调拨信息作废
     *
     * @param transferDetailId 调拨明细id
     * @return 作废信息
     */
    @GetMapping("/invoiceInvalid")
    @ResponseBody
    public R invoiceInvalid(Integer transferDetailId) {
        try {
            LOGGER.info("仓库管理-调拨-单条单据作废 transferDetailId=" + transferDetailId);
            return transferService.invoiceInvalid(transferDetailId);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-单条单据作废异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 调拨数据操作接口
     *
     * @param req 操作对象
     * @return 操作结果
     */
    @PostMapping("/operationTransfer")
    @ResponseBody
    public R operationTransfer(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-调拨-调拨数据操作接口 req=" + req.toString());
            return transferService.operationTransfer(req);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-调拨数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取仓库信息
     *
     * @return
     */
    @GetMapping("/getWarehouse")
    @ResponseBody
    public R getWarehouse() {
        try {
            LOGGER.info("仓库管理-调拨-获取仓库字典信息");
            List<DictionaryInfoVo> warehouseInfos = transferService.getAllWarehouseInfo();
            return R.build(warehouseInfos);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-获取仓库字典信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 调拨新增
     *
     * @param req 新增入参
     * @return 新增结果
     */
    @PostMapping("/add/transfer")
    @ResponseBody
    public R addTransfer(@Validated @RequestBody AddTransferReq req) {
        try {
            LOGGER.info("仓库管理-调拨-调拨新增 req=" + req.toString());
            UserEntity user = (UserEntity) this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return transferService.addTransfer(req, name, code);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-调拨新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 调拨修改
     *
     * @param req 修改入参
     * @return 新增结果
     */
    @PostMapping("/update/transfer")
    @ResponseBody
    public R updateTransfer(@Validated @RequestBody AddTransferReq req) {
        try {
            LOGGER.info("仓库管理-调拨-调拨修改 req=" + req.toString());
            UserEntity user = (UserEntity) this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return transferService.updateTransfer(req, name, code);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-调拨修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 置空新增
     *
     * @param req 新增入参
     * @return 新增结果
     */
    @PostMapping("/add/blanking")
    @ResponseBody
    public R addBlanking(@Validated @RequestBody AddTransferReq req) {
        try {
            LOGGER.info("仓库管理-调拨-置空新增 req=" + req.toString());
            UserEntity user = (UserEntity) this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return transferService.addBlanking(req, name, code);
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-置空新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 外销调拨明细统计
     *
     * @param req 查询条件
     * @return 外销调拨明细统计
     */
    @GetMapping("/statisticsTransferAndContractByPage")
    @ResponseBody
    public R statisticsTransferAndContract(StatisticsTransferAndContractInfoReq req) {
        try {
            LOGGER.info("仓库管理-调拨-外销调拨明细统计 req=" + req.toString());
            PageInfo<StatisticsTransferAndContractInfoVo> pageInfo = transferService.statisticsTransferAndContract(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-外销调拨明细统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 调拨仓库入库详情统计
     * @param req 查询条件
     * @return 调拨仓库入库详情统计
     */
    @GetMapping("/statistics/getStatisticsTransferInStockByPage")
    @ResponseBody
    public R getStatisticsTransferInStockInfo(StatisticsTransferInStockInfoReq req) {
        try {
            LOGGER.info("仓库管理-调拨-调拨仓库入库详情统计 req=" + req.toString());
            PageInfo<StatisticsTransferInStockInfoVo> pageInfo = transferService.getStatisticsTransferInStockInfo(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-调拨-调拨仓库入库详情统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

}
